﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using Hxsoft.AppV1;

namespace Hxsoft.AppV1.Module
{
	/// <summary>
	/// InfoCommentStates 类。
	/// </summary>
	public static partial class InfoCommentStates
	{
		#region GetItem
		/// <summary>
		/// 根据标识获取指定的信息评论状态。
		/// </summary>
		/// <param name="o">信息评论状态集合。</param>
		/// <param name="infoType">获取的信息评论状态标识 InfoType。</param>
		/// <param name="infoId">获取的信息评论状态标识 InfoId。</param>
		/// <returns>获取的信息项。</returns>
		public static InfoCommentState GetItem(this System.Data.Linq.Table<InfoCommentState> o, string infoType, string infoId)
		{
			return o.SingleOrDefault(p => p.InfoType == infoType && p.InfoId == infoId);
		}

		public static InfoCommentState GetItem(string infoType, string infoId)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			dc.ObjectTrackingEnabled = false;
			return dc.InfoCommentStates.GetItem(infoType, infoId);
		}
		#endregion



		#region IsClosedComment
		/// <summary>
		/// 确定指定的信息是否关闭了评论。
		/// </summary>
		/// <param name="infoType">信息的类别。</param>
		/// <param name="infoId">信息的标识。</param>
		/// <returns>布尔值表示指定的信息是否关闭了评论。</returns>
		public static bool IsClosedComment(string infoType, string infoId)
		{
			InfoCommentState state = GetItem(infoType, infoId);
			if (state == null) return true;
			return state.IsClose;
		}
		#endregion



		#region Exists
		/// <summary>
		/// 验证指定的信息评论状态是否已经存在。
		/// </summary>
		/// <param name="o">信息评论状态集合。</param>
		/// <param name="infoType">验证的信息评论状态标识 InfoType。</param>
		/// <param name="infoId">验证的信息评论状态标识 InfoId。</param>
		/// <returns>布尔值表示指定的信息评论状态是否已经存在。</returns>
		public static bool Exists(this System.Data.Linq.Table<InfoCommentState> o, string infoType, string infoId)
		{
			return o.Any(p => p.InfoType == infoType && p.InfoId == infoId);
		}
		#endregion
	}



	/// <summary>
	/// InfoCommentStates 类操作部分。
	/// </summary>
	public static partial class InfoCommentStates
	{
		#region SetInfoCommentState
		public static InfoCommentState SetInfoCommentState(string infoType, string infoId, bool isClose)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			InfoCommentState state = dc.InfoCommentStates.GetItem(infoType, infoId);
			if (state == null)
			{
				state = new InfoCommentState();
				state.InfoType = infoType;
				state.InfoId = infoId;
				state.IsClose = isClose;
				dc.InfoCommentStates.InsertOnSubmit(state);
				dc.SubmitChanges();
			}
			else
			{
				if (state.IsClose != isClose)
				{
					state.IsClose = isClose;

					try
					{
						dc.SubmitChanges(ConflictMode.ContinueOnConflict);
					}
					catch (ChangeConflictException e)
					{
						foreach (ObjectChangeConflict occ in dc.ChangeConflicts)
						{
							occ.Resolve(RefreshMode.KeepCurrentValues);
						}
					}
				}
			}

			return state.ToNewEntity<InfoCommentState>();
		}
		#endregion



		#region Delete
		/// <summary>
		/// 删除信息评论状态。
		/// </summary>
		/// <param name="infoType">删除的信息评论状态标识 InfoType。</param>
		/// <param name="infoId">删除的信息评论状态标识 InfoId。</param>
		public static void Delete(string infoType, string infoId)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();

			InfoCommentState state = dc.InfoCommentStates.GetItem(infoType, infoId);
			if (state == null)
				throw new ObjectNotExistsException(Res.Operation_ObjectNotExists);

			Delete(dc, state);

			dc.SubmitChanges();
		}

		#region internal Delete
		internal static void Delete(GenericModuleDataContext dc, InfoCommentState state)
		{
			dc.InfoCommentStates.DeleteOnSubmit(state);
		}
		#endregion
		#endregion
	}
}